package com.m3958.firstgwt.client.window;

import java.util.LinkedHashMap;

import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.m3958.firstgwt.client.AppStatus;
import com.m3958.firstgwt.client.IhasVoteTplSelector;
import com.m3958.firstgwt.client.constants.VoteTplFieldConstants;
import com.m3958.firstgwt.client.datasource.VoteTplDataSource;
import com.m3958.firstgwt.shared.type.CommonField;
import com.m3958.firstgwt.shared.type.SmartParameterName;
import com.m3958.firstgwt.shared.type.VoteTplField;
import com.m3958.gwtutil.clientside.client.YuiWrapper;
import com.m3958.gwtutil.clientside.client.jso.VoteTplJso;
import com.smartgwt.client.data.Criteria;
import com.smartgwt.client.data.Record;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.SelectionStyle;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.RadioGroupItem;
import com.smartgwt.client.widgets.form.fields.events.ChangedEvent;
import com.smartgwt.client.widgets.form.fields.events.ChangedHandler;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.grid.events.RecordClickEvent;
import com.smartgwt.client.widgets.grid.events.RecordClickHandler;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;

@Singleton
public class VoteTplSelectWindow extends Window{


	private ListGrid grid;
	
	private boolean initialized = false;
	
	private RadioGroupItem radioGroupItem;
	
	@Inject
	private VoteTplFieldConstants vtfContants;
	
	@Inject
	private YuiWrapper yui;
	
	@Inject
	private AppStatus aservice;
	
	@Inject
	private VoteTplDataSource vtds;
	
	private IhasVoteTplSelector requester;
	
	public VoteTplSelectWindow(){
		setWidth(600);
		setHeight(450);
		setIsModal(true);
		setShowModalMask(true);
		setTitle("投票模板选择");
		centerInPage();
	}
	
	public VoteTplSelectWindow showMe(IhasVoteTplSelector requester){
		this.requester = requester;
		if(!initialized){
			initialized = true;
			VLayout layout = new VLayout(5);
			layout.addMember(initCatLayout());
			layout.addMember(initGrid());
			layout.addMember(initBt());
			addItem(layout);
			fetchGridData();
		}
		show();
		return this;
	}
	
	private DynamicForm initCatLayout() {
		DynamicForm form = new DynamicForm();
		form.setWidth100();
		form.setHeight(30);
		radioGroupItem = new RadioGroupItem();
		radioGroupItem.setVertical(false);
		radioGroupItem.setTitle("选择分类：");
		LinkedHashMap<String, String> lm = new LinkedHashMap<String, String>();
		lm.put("system", "系统模板");
		lm.put("public", "公开模板");
		lm.put("private", "我的模板");
		radioGroupItem.setValueMap(lm);
		radioGroupItem.setValue("system");
		radioGroupItem.addChangedHandler(new ChangedHandler() {
			@Override
			public void onChanged(ChangedEvent event) {
				fetchGridData();
			}
		});
		form.setFields(radioGroupItem);
	    return form;
	}
	

	public void fetchGridData() {
		Criteria c = new Criteria();
		String flag = radioGroupItem.getValueAsString();
		yui.logString(radioGroupItem.getValueAsString());
		yui.logString(radioGroupItem.getValue().toString());
		if("public".equals(flag) || "system".equals(flag)){
			c.addCriteria(VoteTplField.FLAG.getValue(), flag);
		}else{
			c.addCriteria(SmartParameterName.FETCH_OWNER, "true");
		}
		c.addCriteria(CommonField.TIMEZONE.getValue(),aservice.getTimeZone());
		grid.filterData(c);
	}
	

	private HLayout initBt() {
		HLayout hl = new HLayout();
		hl.setHeight(25);
		hl.setAlign(Alignment.RIGHT);
		IButton ib = new IButton("关闭");
		ib.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				VoteTplSelectWindow.this.hide();
			}
		});
		hl.addMember(ib);
		return hl;
	}

    
	private ListGrid initGrid() {
		grid = new ListGrid();
		grid.setDataSource(vtds);
		grid.setAutoFetchData(false);
		grid.setSelectionType(SelectionStyle.SINGLE);
		ListGridField idField = new ListGridField("id");

	    ListGridField nameField = new ListGridField(CommonField.NAME.getValue(),vtfContants.name());
	    ListGridField typeField = new ListGridField(VoteTplField.TYPE.getValue(),vtfContants.type());
	    ListGridField tplLangField = new ListGridField(VoteTplField.LANG.getValue(),vtfContants.lang());
	    
	    grid.addRecordClickHandler(new RecordClickHandler() {
			@Override
			public void onRecordClick(RecordClickEvent event) {
				Record r = event.getRecord();
				if(r!=null){
					requester.onVoteTplSelected((VoteTplJso) r.getJsObj());
					VoteTplSelectWindow.this.hide();
				}
			}
		});

		grid.setFields(nameField,typeField,tplLangField,idField);
		return grid;
	}
}
